<script>
	import {
		http,
		ip
	} from '@/config/common.js'
	export default {
		globalData: {
			token: '',
			isLogin: false
		},
		onLaunch: function() {
			console.log('App Launch')
			//从缓存中获取token
			try {
				var data = uni.getStorageSync('token')
				if (data) {
					getApp().globalData.token = data
				}
			} catch (e) {
				// error
				console.log(e)
			}
			//每次打开app去获取用户信息
			http.get('/token/getUserByToken', {
				params: {
					token: getApp().globalData.token
				}
			}).then(res => {
				var data = res.data.data
				if (data === null) {
					getApp().globalData.isLogin = false
					//没有获取到用户信息，清空缓存
					try {
						uni.removeStorageSync('user')
					} catch (e) {
						console.log(err)
					}
					try {
						uni.removeStorageSync('token')
					} catch (e) {
						console.log(err)
					}
				} else {
					//确认是否登录
					if (data.userId) {
						getApp().globalData.isLogin = true
					}
					//缓存user信息
					try {
						uni.setStorageSync('user', data)
					} catch (e) {
						// error
						console.log(e)
					}
				}
			}).catch(err => {
				console.log(err)
			})
		},
		onShow: function() {
			console.log('App Show');
		},
		onHide: function() {
			console.log('App Hide');
		}
	};
</script>

<style lang="scss">
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "@/uni_modules/uview-plus/index.scss";
</style>